/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package db;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import utils.FileUtils;
import utils.Handle;

/**
 *
 * @author jamie
 */
public class MakeSwissProtLookup {

    private File lookupFile = new File("/Users/jamie/monkeys/GO/annotations/entrezSwissProtLookup.txt");

    public void makeLookupTable() {
        Handle handle = Handle.getConnectedHandle("monkeys");
        String sql = "drop table if exists swissProtLookup";
        handle.runSQLupdate(sql);
        sql = "create table swissProtLookup (swissProtAccession varchar(10) not null, gId int(10) not null)";
        handle.runSQLupdate(sql);
        sql = "create index swissProtLookup_index on swissProtLookup(swissProtAccession)";
        handle.runSQLupdate(sql);
        sql = "create index gIdLookup_index on swissProtLookup(gId)";
        handle.runSQLupdate(sql);
        handle.closeConnection();
    }

    public void addData() {
        BufferedReader reader = FileUtils.getBufferedReader(lookupFile);
        String line = null;
        Handle handle = Handle.getConnectedHandle("monkeys");
        boolean first = true;
        try {
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (first) {
                    first = false;
                } else {
                    String split[] = line.split("\t");
                    if(split.length==2){
                        split[0] = split[0].trim();
                        split[1] = split[1].trim();
                        if(split[1].length()>0 && split[0].length()>0){
                            System.out.println(split[1]);
                            String sql = "insert into swissProtLookup values('"+split[0]+"', "+split[1]+")";
                            System.out.println(sql);
                            handle.runSQLupdate(sql);
                        }
                    }
                }
            }
            reader.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        handle.closeConnection();
    }



}
